home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / isobuild / iso_divide.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-11  |  5.4 KB  |  245 lines

  1.  
  2. /* iso_divide.c    routine for computing the vertex locations  */
  3.  
  4. /*  for use with the isobuild program
  5.  *
  6.  * modified by Brian Tierney, LBL  12/90
  7.  *            Lawrence Berkeley Laboratory
  8.  *            Imaging Technologies Group
  9.  *            email: bltierney@lbl.gov
  10.  *
  11.  */
  12.  
  13. /* $Id: iso_divide.c,v 1.3 1992/01/31 02:05:45 tierney Exp $ */
  14.  
  15. /* $Log: iso_divide.c,v $
  16.  * Revision 1.3  1992/01/31  02:05:45  tierney
  17.  * *** empty log message ***
  18.  *
  19.  * Revision 1.2  1991/12/19  01:42:20  davidr
  20.  * added RCS identification markers
  21.  * */
  22.  
  23. static char rcsid[] = "$Id: iso_divide.c,v 1.3 1992/01/31 02:05:45 tierney Exp $" ;
  24.  
  25. #include "isobuild.h"
  26.  
  27. /**************************** iso_surface ****************************/
  28.  
  29. int
  30. iso_divide(startx, starty, endx, endy, slice)
  31.     int       startx, starty, endx, endy, slice;
  32. {
  33.     register int x, y;
  34.     int       index, ly, lx;
  35.     int       npoints = 0;
  36.     int       calc_index_and_temps();
  37.  
  38.     lx = endx - 1;
  39.     ly = endy - 1;
  40.  
  41.     for (y = starty; y < ly; y++)
  42.     for (x = startx; x < lx; x++) {
  43.         index = calc_index(x, y, slice);
  44.  
  45.         if (index > 0) {
  46.         add_point(x, y, slice);
  47.         npoints++;
  48.         }
  49.     }
  50.  
  51.     if (VERBOSE2)
  52.     fprintf(stderr, "slice %d:  %d points \n", slice, npoints);
  53.  
  54.     return (npoints);
  55. }
  56.  
  57. /********************************************************/
  58. int
  59. iso_divide_block(xloc, yloc, zloc, width, height, gb1, gb2, sx, sy, ex, ey)
  60.     int       xloc, yloc, zloc, width, height;
  61.     Grid_type **gb1, **gb2;
  62.     int       sx, sy, ex, ey;
  63. {
  64.     register int i, j, xloc2, yloc2;
  65.     int       npoints = 0, index;
  66.  
  67.     yloc2 = yloc;
  68.  
  69.     for (i = 0; i < height; i++) {
  70.     xloc2 = xloc;
  71.     for (j = 0; j < width; j++) {
  72.  
  73.         if (xloc2 >= sx && yloc2 >= sy && xloc2 < ex && yloc2 < ey) {
  74.  
  75.         index = calc_index_block(xloc2, yloc2, gb1, gb2);
  76.  
  77.         if (index > 0) {/* index of 0 means no triangles */
  78.             add_point(xloc2, yloc2, zloc);
  79.             npoints++;
  80.         }
  81.         }
  82.         xloc2++;
  83.     }
  84.     yloc2++;
  85.     }
  86.  
  87.     return (npoints);
  88. }
  89.  
  90. /********************************************************************/
  91.  
  92. int
  93. calc_index_block(x1, y1, grid_b1, grid_b2)
  94.     int       x1, y1;
  95.     Grid_type **grid_b1, **grid_b2;
  96.  
  97. /* This subroutine calculates the index into the cell_table
  98.  */
  99. {
  100.     int       x2, y2, index = 0;
  101.  
  102.     x2 = x1 + 1;
  103.     y2 = y1 + 1;
  104.  
  105.  
  106.     if (grid_b1[y1][x1] == 1)
  107.     index++;
  108.     if (grid_b1[y1][x2] == 1)
  109.     index += 2;
  110.     if (grid_b1[y2][x2] == 1)
  111.     index += 4;
  112.     if (grid_b1[y2][x1] == 1)
  113.     index += 8;
  114.  
  115.     if (grid_b2[y1][x1] == 1)
  116.     index += 16;
  117.     if (grid_b2[y1][x2] == 1)
  118.     index += 32;
  119.     if (grid_b2[y2][x2] == 1)
  120.     index += 64;
  121.     if (grid_b2[y2][x1] == 1)
  122.     index += 128;
  123.  
  124.  
  125.     /* if index = 0 or 255, then the surface does not intersect this voxel */
  126.     if (index == 0 || index == 255)
  127.     return (0);
  128.  
  129.     return (index);
  130. }
  131.  
  132. /*******************************************************************/
  133. int
  134. calc_index(x1, y1, z1)
  135.     int       x1, y1, z1;
  136. {
  137.     int       x2, y2, z2, index = 0;
  138.  
  139.     x2 = x1 + 1;
  140.     y2 = y1 + 1;
  141.     z2 = z1 + 1;
  142.  
  143.     if (grid[z1][y1][x1] == 1)
  144.     index++;
  145.     if (grid[z1][y1][x2] == 1)
  146.     index += 2;
  147.     if (grid[z1][y2][x2] == 1)
  148.     index += 4;
  149.     if (grid[z1][y2][x1] == 1)
  150.     index += 8;
  151.  
  152.     if (grid[z2][y1][x1] == 1)
  153.     index += 16;
  154.     if (grid[z2][y1][x2] == 1)
  155.     index += 32;
  156.     if (grid[z2][y2][x2] == 1)
  157.     index += 64;
  158.     if (grid[z2][y2][x1] == 1)
  159.     index += 128;
  160.  
  161.     /* if index = 0 or 255, then the surface does not intersect this voxel */
  162.     if (index == 0 || index == 255)
  163.     return (0);
  164.  
  165.     return (index);
  166. }
  167.  
  168. /********************************************************************/
  169.  
  170. POINT_PTR point_list = NULL;
  171. #define BUF_SIZE 100001
  172. int       POINT_LIMIT = BUF_SIZE;
  173. #define POINT_INCR 60000    /* size to increase vertex lists when full */
  174.  
  175. static int num_points = 0;
  176.  
  177. /**************************** add_polygon ****************************/
  178.  
  179. int
  180. add_point(x, y, z)
  181.     int       x, y, z;
  182. {
  183.     NORMAL_VECT norm;
  184.     NORMAL_VECT      calc_normal();
  185.  
  186.     int       size;
  187.  
  188.     if (point_list == NULL) {
  189.     point_list = (POINT_PTR) malloc(sizeof(POINT) * POINT_LIMIT);
  190.     }
  191.  
  192.     /* store the vertices */
  193.     point_list[num_points].x = (u_char) x;    /* x of first point */
  194.     point_list[num_points].y = (u_char) y;    /* y of first point */
  195.     point_list[num_points].z = (u_char) z;    /* z of first point */
  196.  
  197.     if (PRE_NORMALS) {
  198.     point_list[num_points].nx = normals[z][y][x].x;
  199.     point_list[num_points].ny = normals[z][y][x].y;
  200.     point_list[num_points].nz = normals[z][y][x].z;
  201.     } else {
  202.     norm = calc_normal(x, y, z);
  203.     point_list[num_points].nx = norm.x;
  204.     point_list[num_points].ny = norm.y;
  205.     point_list[num_points].nz = norm.z;
  206.     }
  207.  
  208.     num_points++;
  209.  
  210.     if (SMALL_CHUNKS) {
  211.     if (num_points + 3 >= POINT_LIMIT) {
  212.         dump_points(1);    /* write points found so far */
  213.     }
  214.     } else {
  215.     if (num_points + 3 >= POINT_LIMIT) {
  216.         /* get more space */
  217.         POINT_LIMIT += POINT_INCR;    /* add this much space */
  218.         size = POINT_LIMIT * sizeof(POINT);    /* size for malloc/realloc */
  219.         if ((point_list = (POINT_PTR) realloc((char *) point_list,
  220.                           size)) == NULL) {
  221.         Error("not enough memory to store vertices");
  222.         }
  223.     }
  224.     }
  225.     return 0;
  226. }
  227.  
  228. /*********************************************************************/
  229. int
  230. dump_points(more)
  231.     int       more;
  232. {
  233.     void      write_points_to_socket();
  234.  
  235.     if (SERVER) {
  236.     write_points_to_socket(point_list, num_points, more);
  237.     } else if (OUTPUT_TYPE == 0) {
  238.     write_points(point_list, num_points, more);
  239.     } else if (OUTPUT_TYPE == 1) {
  240.     write_points_ascii(point_list, num_points);
  241.     }
  242.     num_points = 0;
  243.     return;
  244. }
  245.